using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class JobSeekers_coverletters : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
             WebConfigurationManager.ConnectionStrings[
               "RecruitVietnam"].ConnectionString;
        if (Session.Contents["jsuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                ShowCoverLetter();
            }
        }
    }
    void ShowCoverLetter()
    {
        
              
        SqlServerProvider cls = 
        new SqlServerProvider(connectionString);
        cls.CommandText = "SPViewCoverLetter";
        cls.CommandType = 
        CommandType.StoredProcedure;
        cls.ParameterCollection = 
        new string[1]{"@UserID"};
        cls.ValueCollection = new 
        string [1]{Convert.ToString(
        Session.Contents["jsuserid"])};
        DataTable dataTable = 
        cls.GetDataTable();
        GridViewHelper helper = 
        new GridViewHelper();
        helper.FillDataTableToGridView(
        dataTable, GridView1);
        MakeUrlForImage(
        dataTable.Rows.Count < 5);
        if (dataTable.Rows.Count == 0)
            btnDelete.Enabled = false;
        if(dataTable.Rows.Count>=5)
            btnNew.Enabled = false;
        else
            btnNew.Enabled = true;
    }
    void MakeUrlForImage(bool enableCopy)
    {
        foreach (GridViewRow gridViewRow in GridView1.Rows)
        {
            string letterID = gridViewRow.Cells[0].Text;
            string letterTitle = ((HyperLink)
            gridViewRow.Cells[2].Controls[0]).Text;
            HyperLink hyperLinkField;
            hyperLinkField = (HyperLink)
                gridViewRow.Cells[5].FindControl("Copy");
            if (enableCopy)
            {                
                hyperLinkField.NavigateUrl =
                "copyletter.aspx?letterid=" 
                + letterID
                + "&title=" + letterTitle;
                hyperLinkField.Text += 
                "Click here to copy Cover Letter";
            }
            else
                hyperLinkField.Text += 
                "You can not copy Cover Letter";
            hyperLinkField = (HyperLink)
            gridViewRow.Cells[6].FindControl("View");
            hyperLinkField.NavigateUrl =
            "viewletter.aspx?letterid=" + 
            letterID;
            hyperLinkField.Text += 
            "Click here to view Cover Letter";
            ImageButton imageButton  = (ImageButton)
                gridViewRow.Cells[7].FindControl(
                "DefaultLetter");
            if (gridViewRow.Cells[0].Text == 
            Session["jscoverletter"].ToString())
            {
                imageButton.ImageUrl = 
                "/icons/check.gif";
                imageButton.ToolTip += 
                "Default Cover Letter";
            }
            else
                imageButton.ToolTip += 
                "Click here to make " +
                "default Cover Letter";
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("coverletter.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DeleteCoverLetter();
        ShowCoverLetter();
    }
    void DeleteCoverLetter()
    {
        string letterID = "";
        foreach (GridViewRow item in 
        GridView1.Rows)
        {
            CheckBox checkBox = (CheckBox)
            item.Cells[0].FindControl(
            "chkCoverLetterID");
            if (checkBox.Checked)
                letterID += item.Cells[0].Text + ",";
        }
        if (letterID != "")
        {
            SqlServerProvider cls = new 
            SqlServerProvider(connectionString);
            cls.CommandText = 
            "delete  from tblJSCoverLetters " +
            " where UserID=@UserID"  
            + " and LetterID in('" +
            letterID.Replace(",", "','") + "')";
            cls.ParameterCollection = 
            new string[1] { "@UserID" };
            cls.ValueCollection = new 
            string[1]{Convert.ToString(
            Session.Contents["jsuserid"])};
            cls.ExecuteNonQuery();
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gridViewRow = 
        GridView1.Rows[e.RowIndex];
        string letterID = gridViewRow.Cells[0].Text;
        if (letterID != "")
            if (DefaultLetter(letterID))
            {
                Session.Contents["jscoverletter"] = letterID;
                ShowCoverLetter();
            }
    }
    bool DefaultLetter(string letterID)
    {
        bool update = false;
        try
        {
            SqlServerProvider cls = new 
            SqlServerProvider(connectionString);
            cls.CommandText = "SPDefaultCoverLetter";
            cls.CommandType= CommandType.StoredProcedure;
            cls.ParameterCollection = 
            new string[2]{"@UserID", "@CoverLetterID"};
            cls.ValueCollection = new string[2]{
            Convert.ToString(
            Session.Contents["jsuserid"]), letterID};
            update = (cls.ExecuteNonQuery() > 0);
        }
        catch (Exception ex)
        {
            literalError.Text = ex.Message;
        }
        return update;
    }
}
